package com.sw.small_world.utils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.application.SWApplication;

import org.w3c.dom.Text;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/23.
 */

public class PopUtils {

    private PopupWindow popupWindow;
    private ImageView one;
    private ImageView two;
    private ImageView three;
    private ImageView four;
    private ImageView five;
    private static int grade = 0;
    private OnSureGradeListener listener;

    public PopupWindow initGradePop(Context context, OnSureGradeListener listener) {
        this.listener = listener;
        View view = View.inflate(context, R.layout.view_grade_pop, null);
        RelativeLayout rlGrade = (RelativeLayout) view.findViewById(R.id.rl_grade);
        ImageView close = (ImageView) view.findViewById(R.id.close_grade_pop);
        one = (ImageView) view.findViewById(R.id.grade_one);
        two = (ImageView) view.findViewById(R.id.grade_two);
        three = (ImageView) view.findViewById(R.id.grade_three);
        four = (ImageView) view.findViewById(R.id.grade_four);
        five = (ImageView) view.findViewById(R.id.grade_five);
        TextView sure = (TextView) view.findViewById(R.id.grade_sure);
        rlGrade.setOnClickListener(onClickListener);
        close.setOnClickListener(onClickListener);
        one.setOnClickListener(onClickListener);
        two.setOnClickListener(onClickListener);
        three.setOnClickListener(onClickListener);
        four.setOnClickListener(onClickListener);
        five.setOnClickListener(onClickListener);
        sure.setOnClickListener(onClickListener);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        rlGrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        return popupWindow;
    }

    PopupWindow popupWindow2;
    ListView pop_list;
    RelativeLayout pop_bg;

    public PopupWindow initMoreListPop(ArrayList<String> groups, AdapterView.OnItemClickListener listener) {
        View layout = View.inflate(SWApplication.getContext(), R.layout.view_pop, null);
        pop_list = (ListView) layout.findViewById(R.id.pop_list);
        pop_bg = (RelativeLayout) layout.findViewById(R.id.pop_bg);
        // 给列表设置Adapter
        PopAdapter groupAdapter = new PopAdapter(groups);
        pop_list.setAdapter(groupAdapter);
        // 创建一个PopuWidow对象
        popupWindow2 = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        // 使其聚集
        popupWindow2.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow2.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popupWindow2.setBackgroundDrawable(new BitmapDrawable());
        // ListView的条目点击事件
        pop_list.setOnItemClickListener(listener);
        pop_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow2 != null) {
                    popupWindow2.dismiss();
                }
            }
        });
        return popupWindow2;
    }

    private PopupWindow popupWindow3;

    public PopupWindow initPublishPop(Context context, View.OnClickListener onClickListener) {
        View view = View.inflate(context, R.layout.view_publish_pop, null);
        RelativeLayout rl_publish = (RelativeLayout) view.findViewById(R.id.rl_publish);
        LinearLayout publishSmallMessage = (LinearLayout) view.findViewById(R.id.publish_small_message);
        LinearLayout publishQuestion = (LinearLayout) view.findViewById(R.id.publish_question);
        LinearLayout publishNeed = (LinearLayout) view.findViewById(R.id.publish_need);
        TextView cancel = (TextView) view.findViewById(R.id.cancel);
        publishSmallMessage.setOnClickListener(onClickListener);
        publishQuestion.setOnClickListener(onClickListener);
        publishNeed.setOnClickListener(onClickListener);
        popupWindow3 = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        // 使其聚集
        popupWindow3.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow3.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popupWindow3.setBackgroundDrawable(new BitmapDrawable());
        rl_publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow3.dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow3.dismiss();
            }
        });
        return popupWindow3;
    }

    static class PopAdapter extends BaseAdapter {
        ArrayList<String> list;


        public PopAdapter(ArrayList<String> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(SWApplication.getContext(), R.layout.item_pop, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvPop.setText(list.get(position));

            return convertView;
        }

        class ViewHolder {
            @Bind(R.id.tv_pop)
            TextView tvPop;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rl_grade:
                case R.id.close_grade_pop:
                    popupWindow.dismiss();
                    break;
                case R.id.grade_one:
                    one.setSelected(true);
                    two.setSelected(false);
                    three.setSelected(false);
                    four.setSelected(false);
                    five.setSelected(false);
                    grade = 1;
                    break;
                case R.id.grade_two:
                    one.setSelected(true);
                    two.setSelected(true);
                    three.setSelected(false);
                    four.setSelected(false);
                    five.setSelected(false);
                    grade = 2;
                    break;
                case R.id.grade_three:
                    one.setSelected(true);
                    two.setSelected(true);
                    three.setSelected(true);
                    four.setSelected(false);
                    five.setSelected(false);
                    grade = 3;
                    break;
                case R.id.grade_four:
                    one.setSelected(true);
                    two.setSelected(true);
                    three.setSelected(true);
                    four.setSelected(true);
                    five.setSelected(false);
                    grade = 4;
                    break;
                case R.id.grade_five:
                    one.setSelected(true);
                    two.setSelected(true);
                    three.setSelected(true);
                    four.setSelected(true);
                    five.setSelected(true);
                    grade = 5;
                    break;
                case R.id.grade_sure:
                    if (grade > 0) {
                        listener.onSure(grade);
                    }
                    popupWindow.dismiss();
                    break;
            }
        }
    };

    public interface OnSureGradeListener {
        void onSure(int grade);
    }

}
